package com.bwsoft.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.bwsoft.domain.Dictionary;
import com.bwsoft.domain.DictionaryData;
import com.bwsoft.model.EasyTree;
import com.bwsoft.model.Message;
import com.bwsoft.service.DictionaryService;

@Service(DictionaryService.NAME)
public class DictionaryServiceImpl extends BaseServiceImpl implements
		DictionaryService {

	private static Logger LOG = Logger.getLogger(DictionaryServiceImpl.class);

	@Override
	public List<EasyTree> findAllByTree() {
		List<EasyTree> treeList = new ArrayList<EasyTree>();
		List<Dictionary> dictionaries = dictionaryDao.findAll();
		for (Dictionary dictionary : dictionaries) {
			EasyTree et = new EasyTree();
			et.setId(dictionary.getDicId());
			et.setText(dictionary.getDescription() + "("
					+ dictionary.getDicName() + ")");
			treeList.add(et);
		}
		return treeList;
	}

	@Override
	public List<DictionaryData> findByPage(Integer page, Integer rows,
			Dictionary dictionary) {
		
		//拼装查询语句
		//String hql = "from Dictionary dic where dic.dicId = ?";
		String hql="from DictionaryData as dicData where dicData.dictionary.dicId=? order by dicData.dicKey asc";
		//List<DictionaryData> dicDataLis=new ArrayList<DictionaryData>();
		Set<DictionaryData> dicDataSet;
		
		//分页查询
		//dicDataSet =((Dictionary)dictionaryDao.findPage(hql, rows, (page - 1) * rows,	dictionary.getDicId()).get(0)).getDictionaryDatas();
		
		List<DictionaryData> dictionaryDatas=dictionaryDataDao.findPage(hql, rows, (page - 1) * rows, dictionary.getDicId());
		return dictionaryDatas;
	}

	@Override
	public int getTotalCount(Dictionary dictionary) {
		
		int count=0;
		
		count=dictionaryDao.findById(dictionary.getDicId()).getDictionaryDatas().size();
		
		return count;
	}

	@Override
	public Message saveOrUpdateDictionary(Dictionary dictionary) {
		Message msg = new Message();
		try {
			dictionaryDao.saveOrUpdate(dictionary);
			msg.setMsgFlag(true);
			msg.setMsgStr("保存成功！");
		} catch (Exception e) {
			msg.setMsgFlag(false);
			msg.setMsgStr("保存失败：" + e.toString());
			LOG.info(e);
		}
		return msg;
	}
	
	@Override
	public Message saveOrUpdateDictionaryData(DictionaryData dictionaryData) {
		Message msg = new Message();
		try {
			dictionaryDataDao.saveOrUpdate(dictionaryData);
			msg.setMsgFlag(true);
			msg.setMsgStr("保存成功！");
		} catch (Exception e) {
			msg.setMsgFlag(false);
			msg.setMsgStr("保存失败：" + e.toString());
			LOG.info(e);
		}
		return msg;
	}

	
	@Override
	public Message checkDictionaryInput(Dictionary dictionary) {
		Message msg = new Message();
		try {
			
			//拼装查询HQL
			StringBuffer sb=new StringBuffer();
			sb.append("from Dictionary dic where 1=1 ");
			if(dictionary.getDicName()!=null&&!dictionary.getDicName().trim().equals("")){
				sb.append(" and dic.dicName='"+dictionary.getDicName()+"'");
			}
			if(dictionary.getDescription()!=null&&!dictionary.getDescription().trim().equals("")){
				sb.append(" and dic.description='"+dictionary.getDescription()+"'");
			}
			if(dictionary.getDicId()!=null){
				sb.append(" and dic.dicId !='"+dictionary.getDicId()+"'");
			}
			LOG.info("检查字典的Form表单重复的HQL"+sb.toString());
			//根据拼装的hql来查询Dictionary集合
			List<Dictionary> dicList=dictionaryDao.findByHql(sb.toString());
			if(dicList.size()==0){
				msg.setMsgFlag(true);
				msg.setMsgStr("数据库中暂无此值");
			}else{
				msg.setMsgFlag(false);
				msg.setMsgStr("数据库已存在此值");
			}
		} catch (Exception e) {
			msg.setMsgFlag(false);
			msg.setMsgStr("添加失败：" + e.toString());
			LOG.info(e);
		}
		return msg;
	}
	
	
	@Override
	public Message checkDictionaryDataInput(DictionaryData dictionaryData) {
		Message msg = new Message();
		try {
			
			//拼装查询HQL
			StringBuffer sb=new StringBuffer();
			sb.append("from DictionaryData dic where dic.dictionary.dicId="+dictionaryData.getDictionary().getDicId());
			if(dictionaryData.getDicKey()!=null&&!dictionaryData.getDicKey().trim().equals("")){
				sb.append(" and dic.dicKey='"+dictionaryData.getDicKey()+"'");
			}
			if(dictionaryData.getDicValue()!=null&&!dictionaryData.getDicValue().trim().equals("")){
				sb.append(" and dic.dicValue='"+dictionaryData.getDicValue()+"'");
			}
			if(dictionaryData.getDicDataId()!=null){
				sb.append(" and dic.dicDataId !='"+dictionaryData.getDicDataId()+"'");
			}
			LOG.info("检查字典的Form表单重复的HQL"+sb.toString());
			//根据拼装的hql来查询DictionaryData集合
			List<DictionaryData> dicDataList=dictionaryDataDao.findByHql(sb.toString());
			if(dicDataList.size()==0){
				msg.setMsgFlag(true);
				msg.setMsgStr("数据库中暂无此值");
			}else{
				msg.setMsgFlag(false);
				msg.setMsgStr("数据库已存在此值");
			}
		} catch (Exception e) {
			msg.setMsgFlag(false);
			msg.setMsgStr("添加失败：" + e.toString());
			LOG.info(e);
		}
		return msg;
	}

	@Override
	public Dictionary dictionaryModifyInfo(Integer dicId) {
		Dictionary dictionary=null;
		try{
			dictionary=dictionaryDao.findById(dicId);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return dictionary;
	}

	@Override
	public Message removeDictionary(Integer dicId) {
		Message msg = new Message();
		List<DictionaryData> dictionaryDatas=new ArrayList<DictionaryData>();
		Dictionary dictionary=dictionaryDao.findById(dicId);
		if(dictionary.getDictionaryDatas().size()>0){
			msg.setMsgFlag(false);
			msg.setMsgStr("删除失败：请先删除该字典项下的所有值！");
		}
		else{
			try{
				dictionaryDao.delete(dictionary);
				msg.setMsgFlag(true);
				msg.setMsgStr("删除成功！");
			}catch (Exception e) {
				msg.setMsgFlag(false);
				msg.setMsgStr("删除失败："+e);
			}
		}
		return msg;
	}

	
	@Override
	public Message removeDictionaryData(Integer dicDataId) {
		Message msg = new Message();
		DictionaryData dicData=dictionaryDataDao.findById(dicDataId);
		try{
				dictionaryDataDao.delete(dicData);
				msg.setMsgFlag(true);
				msg.setMsgStr("删除成功！");
			}catch (Exception e) {
				msg.setMsgFlag(false);
				msg.setMsgStr("删除失败："+e);
			}
		return msg;
	}
	
	@Override
	public DictionaryData dictionaryDataModifyInfo(Integer dicDataId) {
		DictionaryData dictionaryData=null;
		try{
			dictionaryData=dictionaryDataDao.findById(dicDataId);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return dictionaryData;
	}

}
